﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Viettel.VOFFICE.DAO.DAL.Paging;
using Viettel.VOFFICE.DAO.Models;

namespace Viettel.VOFFICE.DAO.DAL.Repository
{
    /// <summary>
    /// lưu chức danh của người dùng trong hệ thống
    /// </summary>
    public class PositionRepository : IRepository<v_Position>
    {
        private VOFFICEContext context;

        public PositionRepository(VOFFICEContext context)
        {
            if (context == null)
            {
                throw new Exception("Context must be init");
            }
            this.context = context;
        }
        public IEnumerable<v_Position> Get()
        {
            return context.v_Position.Where(o => o.IsActive == true);
        }

        public v_Position GetById(object id)
        {
            return context.v_Position.Find(int.Parse(id.ToString()));
        }

        public IEnumerable<v_Position> FindByName(object s)
        {
            throw new NotImplementedException();
        }

        public void Insert(v_Position T)
        {
            context.v_Position.Add(T);
            Save();
        }

        public void Delete(object id)
        {
            var item = GetById(id);
            if (null != item)
            {
                context.v_Position.Attach(item);
                context.v_Position.Remove(item);
                Save();
            }
        }

        public void Delete(v_Position T)
        {
            if (null != T)
            {
                context.v_Position.Attach(T);
                context.v_Position.Remove(T);
                Save();
            }
        }

        public void Update(v_Position T)
        {
            context.Entry(T).State = EntityState.Modified;
            Save();
        }

        public void Save()
        {
            context.SaveChanges();
        }
        public PagedList<v_Position> Pading(int pageIndex, int pageSize)
        {
            return context.v_Position.OrderByDescending(o => o.UpdatedAt).Paginate(pageIndex, pageSize);
        }
    }
}
